package top.huntwolf.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.huntwolf.admin.annotation.URLControl;
import top.huntwolf.admin.annotation.URLMessage;
import top.huntwolf.admin.common.JsonResult;
import top.huntwolf.admin.common.enums.CodeMsg;
import top.huntwolf.admin.pojo.orderEntity.Order;
import top.huntwolf.admin.service.OrderService;

import java.util.List;
@URLControl
@Api(description = "订单控制器")
@RestController
@CrossOrigin
@RequestMapping(value = "/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @URLMessage(url = "/order/**")
    @DeleteMapping("/{order_id}")
    public JsonResult deleteOrder(@PathVariable String order_id){
        orderService.deleteOrder(order_id);
        return JsonResult.success(CodeMsg.SUCCESS);
    }

    @PostMapping("/buyOrder")
    @ApiOperation("生成订单")
    public JsonResult<String> generateBuyOrder(@RequestBody Order order){
            return JsonResult.success(orderService.generateTemporaryOrder(order));
    }

    @GetMapping("/buyBooksInOrder/{orderId}")
    @ApiOperation("根据orderId获取订单")
    public JsonResult<Order> queryBuyBooksInOrder( @PathVariable String orderId){
        return JsonResult.success(orderService.queryBooksInOrder(orderId));
    }

    @GetMapping("/userBuyOrder")
    @ApiOperation("查询用户所有买书订单")
    public JsonResult<List<Order>> queryOrdersByUser(){
        List<Order> orders = orderService.queryBuyOrdersByUser();
        return JsonResult.success(orders);
    }

    @PutMapping("/updateOrder")
    @ApiOperation("更新订单信息")
    public JsonResult updateOrder(@RequestBody Order order){
        orderService.updateOrder(order);
        return JsonResult.success(null);
    }

    @PutMapping("/cancelOrder")
    @ApiOperation("取消订单,status:0为待付款，1为待发货，2为待收货，3为退款/售后,4为待初审，5为待取件，6为待审核，7为待收款，8为已完成,9为取消订单,10为未完成")
    public JsonResult cancelOrder(@RequestBody Order order){

        orderService.generateCancelOrder(order.getOrderId());
        return JsonResult.success(CodeMsg.SUCCESS);
    }

    @PutMapping("/refundOrder")
    @ApiOperation("申请退款")
    public JsonResult refundOrder(@RequestBody Order order){
        orderService.refundOrder(order);
        return JsonResult.success(CodeMsg.SUCCESS);
    }

}
